﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;

public partial class Combat_BaseSkillNew : System.Web.UI.Page
{
    string id = "55f5139f-489a-487d-8d55-ff6e555c7fda";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in SkillList.Items)
        {
            if (item.Selected&&(!item.Value.Equals("**********")))
            {
                ItemSkillInfo info = new ItemSkillInfo();
                info.ID = Guid.NewGuid();
                info.AlisSkillName = item.Text.Trim();
                info.ItemSkillName = item.Text.Trim();
                info.ParentID =new Guid(ddlItemList.SelectedValue.ToString());
                info.SkillID = new Guid(item.Value);
                info.ItemLevel = 2;
                ItemSkill.Insert(info);

            }
        }
        BindSelect(ddlItemList.SelectedValue);

    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        foreach (ListItem item in SelectList.Items)
        {
            if (item.Selected)
            {
                ItemSkill.Delete(new Guid(item.Value));
            }
        }
        BindSelect(ddlItemList.SelectedValue);
    }

    private void Bind()
    {
        List<ItemSkillInfo> skillList = ItemSkill.GetList(" ParentID='" + id + "' OR ParentID IN(Select ID From ItemSkill Where ParentID='" + id + "')");
        List<ItemSkillInfo> skillListpt = skillList.Where(o => o.ParentID == new Guid(id)).ToList<ItemSkillInfo>();
        foreach (ItemSkillInfo info in skillListpt)
        {
            ddlItemList.Items.Add(new ListItem(info.ItemSkillName, info.ID.ToString()));
            List<ItemSkillInfo> skillListchild = skillList.Where(o => o.ParentID == info.ID).ToList<ItemSkillInfo>();
            foreach (ItemSkillInfo iminfo in skillListchild)
            {
                ddlItemList.Items.Add(new ListItem("".PadLeft(2 * iminfo.ItemLevel, '　') + iminfo.ItemSkillName + "[" + iminfo.AlisSkillName + "]", iminfo.ID.ToString()));
            }
        }

        if (!string.IsNullOrEmpty(Request["ID"]))
        {
            ddlItemList.SelectedValue = Request["ID"];
        }

        SkillList.DataSource= TaskSkill.GetList(" 1=1 Order by OrderID");
        SkillList.DataTextField = "SkillName";
        SkillList.DataValueField = "SkillID";
        SkillList.DataBind();
        BindSelect(ddlItemList.SelectedValue);
    }

    private void BindSelect(string parentid)
    {
        SelectList.DataSource = ItemSkill.GetList(" ParentID='" + parentid + "'");
        SelectList.DataTextField = "ItemSkillName";
        SelectList.DataValueField = "ID";
        SelectList.DataBind();
    }
    protected void ddlItemList_SelectedIndexChanged(object sender, EventArgs e)
    {        
        ItemSkillInfo iteminfo = ItemSkill.GetItemSkill(new Guid(ddlItemList.SelectedValue));
        if (iteminfo != null && iteminfo.ItemLevel == 0)
        {
            ButtonAdd.Visible = false;
            ButtonRemove.Visible = false;
            return;

        }
        else
        {
            ButtonAdd.Visible = true;
            ButtonRemove.Visible = true;
            BindSelect(ddlItemList.SelectedValue);
        }
        
    }
    protected void ddlRank_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
